import { message } from 'antd';
import { getList, getDetail, getEmpList } from '@/services/incexpcompare';

export default {
  state: {
    formFieldsValue: {},
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '客户账单', isHas: false },
      { itemCode: '2', itemName: '供应商帐单', isHas: false },
    ],
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailInfo: {},
    valuesArr: [
      'sendSuppId',
      'billYm',
      'budgState',
    ]
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
      }
    },
    // 获取详情数据
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        yield put({
          type: 'updateModalData',
          payload: { detailInfo: response.data },
        });
      } else {
        message.warning(response.message);
      }
    },
    // 获取人员明细列表
    *fetchEmpList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || '0', current: currentPage, pageSize },
        },
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
